/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.AdvertiseDAO;
import DAO.PaymentDAO;
import Models.Advertise;
import Models.Payment;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyro
 */
@ManagedBean
@RequestScoped
public class AdvertiseController {

    public String processRegister() {
        Advertise a = (Advertise) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("advertiseBean"));
        Payment payment = (Payment) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("paymentBean"));

        new AdvertiseDAO().insertAdvertise(a);
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int money;
        if (payment.getExtensionType().equalsIgnoreCase("Monthly")) {
            money = payment.getAmount() * 15;
        } else {
            money = payment.getAmount() * 40;
        }
        payment.setMoney(money);
        session.setAttribute("Money", money);
        payment.setAdvertiseID(new AdvertiseDAO().getCurrentID());
        new PaymentDAO().addPayment(payment);
        if (payment.getPaymentType().equals("Cash")) {
            return "PayByCash";
        } else {
            return "PayByBank";
        }
    }

    /**
     * Creates a new instance of AdvertiseController
     */
    public AdvertiseController() {
    }
    ArrayList<Advertise> listAdvertise;

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public ArrayList<Advertise> getListAdvertise() {
        listAdvertise = new AdvertiseDAO().getAllAdvertise();
        return listAdvertise;
    }

    public void deleteAdvertise() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        new AdvertiseDAO().deleteAdvertiseByID(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Advertise has been deleted !"));
    }
}
